<?php


namespace App\Logic\reservation;


use App\Models\LoginModel;
use App\Models\Patient;
use Elasticsearch\ClientBuilder;

class EsHighlight
{
    public static function higHlight($userName)
    {
        //用户名
        if (!empty($userName)) {
            $host = ['127.0.0.1:9200'];
            $client = ClientBuilder::create()
                ->setHosts($host)
                ->build();
            $params = [
                'index' => 'username',

                'body' => [
                    'query' => [
                        'match' => [
                            'username' => [
                                'query' => $userName
                            ]
                        ]
                    ],
                    'highlight' => [
                        'fields' => [
                            'username' => [
                                'pre_tags' => ["<h1><span style='color: red''>"],
                                'post_tags' => ["</span></h1>"]
                            ]
                        ]
                    ]
                ]
            ];
            $response = $client->search($params);
//            dd($response);
            $data = $response['hits']['hits'];
//            dd($data);
            return $data;
        } else {
            $data = Patient::paginate(10);
            return  $data;
        }
    }

}
